package com.shntec.bp.impl;

import javax.servlet.http.HttpServletRequest;

import com.shntec.bp.common.NormalRequestDispatcher;
import com.shntec.bp.common.RequestMessageParser;
import com.shntec.bp.common.ResponseMessageGenerator;
import com.shntec.bp.common.ShntecErrorCode;
import com.shntec.bp.exception.ShntecException;

import com.shntec.bp.util.ShntecLogger;

public class ShntecBpEntry {
	
	
	// Based on JSON message
	public ResponseMessageGenerator processRequest(String jsonRequest, HttpServletRequest request) throws ShntecException {
		
		ShntecLogger.logger.debug("Enter ShntecBpEntry processRequest() ... ");
		
		// Parse received request message, check JSON message format  
		RequestMessageParser parser = new RequestMessageParser();
		
		if (!parser.parse(jsonRequest)) {
			throw new ShntecException(ShntecErrorCode.SHNTEC_ERROR_CODE_BAD_JSON);
		}
		
		parser.setRequest(request);
		
		NormalRequestDispatcher  dispatcher = null;
		ResponseMessageGenerator generator = null;
		
		dispatcher = new NormalRequestDispatcher();
		generator = dispatcher.dispatcherHandler(parser);
		
		if (null == generator) {
			ShntecLogger.logger.error("JSON request message process failed.");
			throw new ShntecException(ShntecErrorCode.SHNTEC_ERROR_CODE_SYSTEM_ERROR);
		}
		
		
		//jsonResponse = generator.generate();
		
		ShntecLogger.logger.debug("Leave ShntecBpEntry processRequest() ... ");

		return generator;
	}

}
